/*-
 * #%L
 * HAPI FHIR JPA Server
 * %%
 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package ca.uhn.fhir.jpa.entity;

import ca.uhn.fhir.jpa.model.entity.BasePartitionable;
import ca.uhn.fhir.jpa.model.entity.IdAndPartitionId;
import ca.uhn.fhir.util.ValidateUtil;
import jakarta.annotation.Nonnull;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.length;

@Entity
@Table(
		name = "TRM_CONCEPT_MAP_GROUP",
		indexes = {@Index(name = "FK_TCMGROUP_CONCEPTMAP", columnList = "CONCEPT_MAP_PID")})
@IdClass(IdAndPartitionId.class)
public class TermConceptMapGroup extends BasePartitionable implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id()
	@SequenceGenerator(name = "SEQ_CONCEPT_MAP_GROUP_PID", sequenceName = "SEQ_CONCEPT_MAP_GROUP_PID")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CONCEPT_MAP_GROUP_PID")
	@Column(name = "PID")
	private Long myId;

	@ManyToOne()
	@JoinColumns(
			value = {
				@JoinColumn(
						name = "CONCEPT_MAP_PID",
						insertable = false,
						updatable = false,
						nullable = false,
						referencedColumnName = "PID"),
				@JoinColumn(
						name = "PARTITION_ID",
						referencedColumnName = "PARTITION_ID",
						insertable = false,
						updatable = false,
						nullable = false)
			},
			foreignKey = @ForeignKey(name = "FK_TCMGROUP_CONCEPTMAP"))
	private TermConceptMap myConceptMap;

	@Column(name = "CONCEPT_MAP_PID", nullable = false)
	private Long myConceptMapPid;

	@Column(name = "SOURCE_URL", nullable = false, length = TermCodeSystem.MAX_URL_LENGTH)
	private String mySource;

	@Column(name = "SOURCE_VERSION", nullable = true, length = TermCodeSystemVersion.MAX_VERSION_LENGTH)
	private String mySourceVersion;

	@Column(name = "TARGET_URL", nullable = false, length = TermCodeSystem.MAX_URL_LENGTH)
	private String myTarget;

	@Column(name = "TARGET_VERSION", nullable = true, length = TermCodeSystemVersion.MAX_VERSION_LENGTH)
	private String myTargetVersion;

	@OneToMany(mappedBy = "myConceptMapGroup")
	private List<TermConceptMapGroupElement> myConceptMapGroupElements;

	@Column(name = "CONCEPT_MAP_URL", nullable = true, length = TermConceptMap.MAX_URL_LENGTH)
	private String myConceptMapUrl;

	@Column(name = "SOURCE_VS", nullable = true, length = TermValueSet.MAX_URL_LENGTH)
	private String mySourceValueSet;

	@Column(name = "TARGET_VS", nullable = true, length = TermValueSet.MAX_URL_LENGTH)
	private String myTargetValueSet;

	public TermConceptMap getConceptMap() {
		return myConceptMap;
	}

	public TermConceptMapGroup setConceptMap(TermConceptMap theTermConceptMap) {
		myConceptMap = theTermConceptMap;
		myConceptMapPid = theTermConceptMap.getId();
		Validate.notNull(myConceptMapPid, "Concept map pid must not be null");
		setPartitionId(theTermConceptMap.getPartitionId());
		return this;
	}

	public List<TermConceptMapGroupElement> getConceptMapGroupElements() {
		if (myConceptMapGroupElements == null) {
			myConceptMapGroupElements = new ArrayList<>();
		}

		return myConceptMapGroupElements;
	}

	public String getConceptMapUrl() {
		if (myConceptMapUrl == null) {
			myConceptMapUrl = getConceptMap().getUrl();
		}
		return myConceptMapUrl;
	}

	public Long getId() {
		return myId;
	}

	public String getSource() {
		return mySource;
	}

	public TermConceptMapGroup setSource(@Nonnull String theSource) {
		ValidateUtil.isNotBlankOrThrowIllegalArgument(theSource, "theSource must not be null or empty");
		ValidateUtil.isNotTooLongOrThrowIllegalArgument(
				theSource,
				TermCodeSystem.MAX_URL_LENGTH,
				"Source exceeds maximum length (" + TermCodeSystem.MAX_URL_LENGTH + "): " + length(theSource));
		this.mySource = theSource;
		return this;
	}

	public String getSourceValueSet() {
		if (mySourceValueSet == null) {
			mySourceValueSet = getConceptMap().getSource();
		}
		return mySourceValueSet;
	}

	public String getSourceVersion() {
		return mySourceVersion;
	}

	public TermConceptMapGroup setSourceVersion(String theSourceVersion) {
		ValidateUtil.isNotTooLongOrThrowIllegalArgument(
				theSourceVersion,
				TermCodeSystemVersion.MAX_VERSION_LENGTH,
				"Source version ID exceeds maximum length (" + TermCodeSystemVersion.MAX_VERSION_LENGTH + "): "
						+ length(theSourceVersion));
		mySourceVersion = theSourceVersion;
		return this;
	}

	public String getTarget() {
		return myTarget;
	}

	public TermConceptMapGroup setTarget(@Nonnull String theTarget) {
		ValidateUtil.isNotBlankOrThrowIllegalArgument(theTarget, "theTarget must not be null or empty");
		ValidateUtil.isNotTooLongOrThrowIllegalArgument(
				theTarget,
				TermCodeSystem.MAX_URL_LENGTH,
				"Target exceeds maximum length (" + TermCodeSystem.MAX_URL_LENGTH + "): " + length(theTarget));
		this.myTarget = theTarget;
		return this;
	}

	public String getTargetValueSet() {
		if (myTargetValueSet == null) {
			myTargetValueSet = getConceptMap().getTarget();
		}
		return myTargetValueSet;
	}

	public String getTargetVersion() {
		return myTargetVersion;
	}

	public TermConceptMapGroup setTargetVersion(String theTargetVersion) {
		ValidateUtil.isNotTooLongOrThrowIllegalArgument(
				theTargetVersion,
				TermCodeSystemVersion.MAX_VERSION_LENGTH,
				"Target version ID exceeds maximum length (" + TermCodeSystemVersion.MAX_VERSION_LENGTH + "): "
						+ length(theTargetVersion));
		myTargetVersion = theTargetVersion;
		return this;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
				.append("myId", myId)
				.append(myConceptMap != null ? ("myConceptMap - id=" + myConceptMap.getId()) : ("myConceptMap=(null)"))
				.append("mySource", mySource)
				.append("mySourceVersion", mySourceVersion)
				.append("myTarget", myTarget)
				.append("myTargetVersion", myTargetVersion)
				.append(
						myConceptMapGroupElements != null
								? ("myConceptMapGroupElements - size=" + myConceptMapGroupElements.size())
								: ("myConceptMapGroupElements=(null)"))
				.append("myConceptMapUrl", this.getConceptMapUrl())
				.append("mySourceValueSet", this.getSourceValueSet())
				.append("myTargetValueSet", this.getTargetValueSet())
				.toString();
	}
}
